package com.utils.oss;

import java.io.InputStream;
import java.util.Date;

import com.aliyun.oss.OSSClient;

import com.utils.basehelper.date.DateUtil1;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class UploadOSS {

	private static final String ossTTPre = "novel/user/avatar/";
	private static final String o = "/";
	
	/**
	 * 上传图片/视频到阿里云OSS服务器,
	 * @param prefixPath
	 * @param strdate
	 */
	public static String uploadCommon(InputStream inputStream, String fileName,
			String endpoint, String accessKeyId, String accessKeySecret, String bucketName) {
		log.info("OSS fileName:{},endpoint:{},accessKeyId:{},accessKeySecret:{},bucketName:{}"
				,fileName,endpoint,accessKeyId,accessKeySecret,bucketName);
		Date date = new Date();
		String ymd = DateUtil1.formatString(date, DateUtil1.YMD_S);
		String prefix = ossTTPre + ymd + o;

		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		if (inputStream != null) {
			ossClient.putObject(bucketName, prefix + fileName, inputStream);
			log.info("user upload image:{}",prefix + fileName);
		}
		ossClient.shutdown();
		return prefix + fileName;
	}
	
	/**
	 * 上传图片/视频到阿里云OSS服务器,自定义路径前缀
	 * @param prefixPath
	 * @param strdate
	 */
	public static String uploadCommon(InputStream inputStream, String prefix, String fileName,
			String endpoint, String accessKeyId, String accessKeySecret, String bucketName) {
		log.info("OSS prefix:{},fileName:{},endpoint:{},accessKeyId:{},accessKeySecret:{},bucketName:{}",
				prefix,fileName,endpoint,accessKeyId,accessKeySecret,bucketName);
		if(!prefix.endsWith(o)) {
			prefix = prefix+o;
		}
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		if (inputStream != null) {
			ossClient.putObject(bucketName, prefix + fileName, inputStream);
//			log.info("our picture upload image:{}",prefix + fileName);
		}
		
//		ObjectMetadata meta = new ObjectMetadata();
//		meta.setContentDisposition("attachment");//设置URl强制下载
//		ossClient.putObject(bucketName, prefixPath + fileName, inputStream,meta);
//		log.info("user upload file:{}",prefixPath + fileName);
		
		ossClient.shutdown();
		return prefix + fileName;
	}

}
